<html>
<head><meta charset="utf-8"><title>borrow rules · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/borrow.20rules.html">borrow rules</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212789862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/borrow%20rules/near/212789862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Haris Muzaffar <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/borrow.20rules.html#212789862">(Oct 09 2020 at 07:10)</a>:</h4>
<p>I have a simple stupid questions:<br>
Consider the following two snippets of code</p>
<div class="codehilite"><pre><span></span><code>let mut a = 90;
let aa = &amp;a;
let aaa = &amp;mut a;
println!(&quot;{}&quot;, aaa);
</code></pre></div>


<p>and</p>
<div class="codehilite"><pre><span></span><code>let mut a = 90;
let aaa = &amp;mut a;
let aa = &amp;a;
println!(&quot;{}&quot;, aaa);
</code></pre></div>


<p>Why does Rust complain in the second snippet that "cannot borrow <code>a</code> as immutable because it is also borrowed" but not in the first one?</p>



<a name="212790143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/borrow%20rules/near/212790143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Stu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/borrow.20rules.html#212790143">(Oct 09 2020 at 07:15)</a>:</h4>
<p>In the first snippet, the immutable reference is not used after the mutable reference, so it gets dropped before the mutable reference is created. This is called non-lexical lifetimes</p>



<a name="212790231"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/borrow%20rules/near/212790231" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Stu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/borrow.20rules.html#212790231">(Oct 09 2020 at 07:16)</a>:</h4>
<p>There are more details here <a href="https://doc.rust-lang.org/stable/edition-guide/rust-2018/ownership-and-lifetimes/non-lexical-lifetimes.html">https://doc.rust-lang.org/stable/edition-guide/rust-2018/ownership-and-lifetimes/non-lexical-lifetimes.html</a></p>



<a name="212795182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/borrow%20rules/near/212795182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Haris Muzaffar <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/borrow.20rules.html#212795182">(Oct 09 2020 at 08:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="230287">Stu</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/borrow.20rules/near/212790143">said</a>:</p>
<blockquote>
<p>In the first snippet, the immutable reference is not used after the mutable reference, so it gets dropped before the mutable reference is created. This is called non-lexical lifetimes</p>
</blockquote>
<p>To add to your answer, i found a similiar question on stackoverflow and it is worth reading <br>
<a href="https://stackoverflow.com/questions/57620041/how-does-lifetime-rule-stops-multiple-mutable-borrow">https://stackoverflow.com/questions/57620041/how-does-lifetime-rule-stops-multiple-mutable-borrow</a></p>



<a name="212825975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/borrow%20rules/near/212825975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/borrow.20rules.html#212825975">(Oct 09 2020 at 13:35)</a>:</h4>
<p>Please ask these sorts of questions in <a class="stream" data-stream-id="122651" href="/#narrow/stream/122651-general">#general</a> , this stream is for help working on the compiler itself.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>